package fr.ul.ade.prime.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
import android.util.Log;
import fr.ul.ade.prime.model.ScheduleState;
import fr.ul.ade.prime.service.ScheduleUpdateProgress;

/**
 * @author Schnell Michaël
 * @ersion 1.0
 */
public class ScheduleUpdateDialog implements Runnable, OnCancelListener {

	protected static final String TAG = ScheduleUpdateDialog.class
			.getSimpleName();

	protected static final int PROGRESS_UPDATE_DELAY = 15;

	protected static final int MAX_PROGRESS = 100;

	protected int step;

	protected int maxProgress;

	protected int currentProgress;

	protected Handler handler;

	protected ProgressDialog dialog;

	protected Activity activity;

	protected final Object lock;

	public ScheduleUpdateDialog(Activity activity, int titleId) {
		super();
		this.lock = new Object();
		this.step = 1;
		this.handler = new Handler();
		this.handler.postDelayed(this, PROGRESS_UPDATE_DELAY);
		this.activity = activity;
		this.dialog = new ProgressDialog(activity);
		this.dialog.setTitle(titleId);
		this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		this.dialog.setOnCancelListener(this);
		this.init();
		this.maxProgress = 0;
	}// ScheduleUpdateDialog(Context, int)

	public ScheduleUpdateDialog(Activity activity, int titleId,
			ScheduleState state) {
		super();
		this.lock = new Object();
		this.step = 1;
		this.handler = new Handler();
		this.handler.postDelayed(this, PROGRESS_UPDATE_DELAY);
		this.activity = activity;
		this.dialog = new ProgressDialog(activity);
		this.dialog.setTitle(titleId);
		this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		this.dialog.setOnCancelListener(this);
		this.init();
		this.maxProgress = getMaxProgress(state);
	}// ScheduleUpdateDialog(Context, int)

	public void init() {
		synchronized (this.lock) {
			this.maxProgress = 0;
			this.currentProgress = 0;
			this.dialog.setProgress(0);
			if (!this.dialog.isShowing()) {
				this.dialog.show();
			}// if
		}// synchronized
	}// init()

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void run() {
		synchronized (this.lock) {
			if (this.currentProgress < this.maxProgress) {
				this.currentProgress += this.step;
				this.dialog.incrementProgressBy(this.step);
			}// if
		}// synchronized
		if (this.currentProgress < MAX_PROGRESS) {
			this.handler.postDelayed(this, PROGRESS_UPDATE_DELAY);
		} else {
			this.dialog.dismiss();
		}// if
	}// run()

	public void updateMaxProgress(ScheduleState state) {
		if (state != null) {
			synchronized (this.lock) {
//				Log.d("UpdateDialog", String.format( //$NON-NLS-1$
//						"[updateMaxProgress]: max set to %3d from %3d (%s)", //$NON-NLS-1$ 
//						Integer.valueOf(this.maxProgress + state.getWeight()),
//						Integer.valueOf(this.maxProgress), state));
				this.maxProgress = state.getWeight();
			}// synchronized
		}// if
	}// updateMaxProgress(ScheduleUpdateDialog)

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onCancel(DialogInterface dialogInterface) {
		this.currentProgress = MAX_PROGRESS;
		dialogInterface.dismiss();
		this.activity.onBackPressed();
	}// onCancel(DialogInterface)

	public void dismiss() {
		this.currentProgress = MAX_PROGRESS;
		this.dialog.dismiss();
	}// dismiss()

	protected static int getMaxProgress(ScheduleState state) {
		return state.getWeight();
	}// getMaxProgress(ScheduleUpdateProgress)

}// class ScheduleUpdateDialog